package cn.fintecher.gis.service;

import cn.fintecher.gis.entity.LandQuo;
import cn.fintecher.gis.entity.QLandQuo;
import cn.fintecher.gis.enums.LandType;
import cn.fintecher.gis.enums.Rapporttype;
import cn.fintecher.gis.respository.LandQuoRepository;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service("landQuoService")
public class LandQuoService {

    private final Logger log = LoggerFactory.getLogger(LandQuoService.class);

    @Autowired
    private LandQuoRepository quoRepository;

    /**
     * 查询所有一级的编号
     * @param reportType
     * @return
     */
    public List<LandQuo> getLandQuoByCode(Rapporttype reportType){
        BooleanBuilder builder = new BooleanBuilder();
        QLandQuo qLandQuo = QLandQuo.landQuo;
        builder.and(qLandQuo.grade.eq(1));
        if (reportType.equals(Rapporttype.LAND_CLASSIFICATION)) {  //土地利用现状一级分类统计
            builder.and(qLandQuo.type.eq(LandType.LAND_QUO));//土地利用现状
        } else if (reportType.equals(Rapporttype.LAND_USE_AREA)) { //土地用途区统计
            builder.and(qLandQuo.type.eq(LandType.LAND_USE));
        } else if (reportType.equals(Rapporttype.LAND_CONSTRUCTION)) { //建设用地管制区
            builder.and(qLandQuo.type.eq(LandType.LAND_CONSTRUCTION));
        } else {
            builder.and(qLandQuo.type.eq(LandType.LAND_PLANNING));//土地规划
        }
        Iterator<LandQuo> iterator = quoRepository.findAll(builder).iterator();
        return Lists.newArrayList(iterator);
    }

    /**
     * 土地利用现状（二级查询）
     * @param parentId
     * @return
     */
    public List<LandQuo> getSecondeLandQuo(String parentId){
        BooleanBuilder builder = new BooleanBuilder();
        QLandQuo qLandQuo = QLandQuo.landQuo;
        builder.and(qLandQuo.grade.eq(2));
        builder.and(qLandQuo.type.eq(LandType.LAND_QUO));
        builder.and(qLandQuo.parentId.eq(parentId));
        Iterator<LandQuo> iterator = quoRepository.findAll(builder).iterator();
        return Lists.newArrayList(iterator);
    }


    /**
     * 根据编号和类型查询对应的信息
     *
     * @param code
     * @param reportType
     * @return
     */
    public List<LandQuo> getLandQuoByCode(String code, Rapporttype reportType) {
        BooleanBuilder builder = new BooleanBuilder();
        QLandQuo qLandQuo = QLandQuo.landQuo;
        builder.and(qLandQuo.code.trim().eq(code));
        if (reportType.equals(Rapporttype.LAND_CLASSIFICATION)) {  //土地利用现状一级分类统计
            builder.and(qLandQuo.type.eq(LandType.LAND_QUO));//土地利用现状
        } else if (reportType.equals(Rapporttype.LAND_USE_AREA)) { //土地用途区统计
            builder.and(qLandQuo.type.eq(LandType.LAND_USE));
        } else if (reportType.equals(Rapporttype.LAND_CONSTRUCTION)) { //建设用地管制区
            builder.and(qLandQuo.type.eq(LandType.LAND_CONSTRUCTION));
        } else {
            builder.and(qLandQuo.type.eq(LandType.LAND_PLANNING));//土地规划
        }
        Iterator<LandQuo> iterator = quoRepository.findAll(builder).iterator();
        return Lists.newArrayList(iterator);
    }


    /**
     * 根据id查询编号信息
     *
     * @param id
     * @return
     */
    public LandQuo getLandQuoById(String id) {
        LandQuo landQuo = new LandQuo();
        quoRepository.findById(id).ifPresent(quo -> {
            BeanUtils.copyProperties(quo, landQuo);
            return;
        });
        return landQuo;
    }
}
